#include "automatic_mode.h"
#include "ui_automatic_mode.h"

AutomaticMode::AutomaticMode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AutomaticMode)
{
    ui->setupUi(this);

    initializeBackground(ui->origin_widget_Logo);

    ui->frame->setStyleSheet("QFrame{background-color: rgb(229, 232, 237);border-radius:10px}");

    setLabelWithBackground(ui->frame, "原位置", Qt::green, 100, 10);

    ui->stackedWidget->setCurrentIndex(0);

    connectButtonsAndSignals();
    setLED(ui->label_293, 1, 16);
    setLED(ui->label_263, 2, 16);
    setSpeedValue(ui->speed_flexibleLabel);

    connect(ui->pb_addSpeed, &QPushButton::clicked, this, &AutomaticMode::addSpeedClicked);
    connect(ui->pb_decSpeed, &QPushButton::clicked, this, &AutomaticMode::decSpeedClicked);
    connect(ui->automaticReturnMenu, &QPushButton::clicked, this, &AutomaticMode::pushReturnMenu);

    ui->speed_flexibleLabel->setText(pendantInterfaceData.speedMutableLabel);

}

void AutomaticMode::initializeBackground(QWidget *widget)
{
    widget->setAutoFillBackground(true);
    QPalette palette = widget->palette();
    QPixmap background(":/image/AEF_LOGO.jpeg");
    QPixmap scaledBackground = background.scaled(widget->size(), Qt::IgnoreAspectRatio);
    palette.setBrush(QPalette::Background, scaledBackground);
    widget->setPalette(palette);

    ui->widget_2->setAutoFillBackground(true);
    palette = ui->widget_2->palette();
    palette.setColor(QPalette::Background, Qt::white);
    ui->widget_2->setPalette(palette);
    ui->widget_3->setAutoFillBackground(true);
    palette = ui->widget_2->palette();
    palette.setColor(QPalette::Background, Qt::white);
    ui->widget_3->setPalette(palette);
}

void AutomaticMode::setLabelWithBackground(QWidget *widget, const QString &text, QColor backgroundColor, int x, int y)
{
    QLabel* label = new QLabel(widget);
    label->setText(text);
    label->move(x, y);
    label->setStyleSheet("QLabel{background-color: " + backgroundColor.name() + ";}");
}

void AutomaticMode::connectButtonsAndSignals()
{
    connect(ui->pb_ioStatus, &QPushButton::clicked, this, [&](){
       ui->stackedWidget->setCurrentIndex(0);
    });

    connect(ui->pb_calTime, &QPushButton::clicked, this, [&](){
       ui->stackedWidget->setCurrentIndex(1);
    });

    connect(ui->pb_promanage, &QPushButton::clicked, this, [&](){
       ui->stackedWidget->setCurrentIndex(2);
    });

    connect(ui->pb_lastIoPage, &QPushButton::clicked, this, [&](){
        if(ui->stackedWidget_2->currentIndex() == 1)
            ui->stackedWidget_2->setCurrentIndex(0);
    });

    connect(ui->pb_nextIoPage, &QPushButton::clicked, this, [&](){
        if(ui->stackedWidget_2->currentIndex() == 0)
            ui->stackedWidget_2->setCurrentIndex(1);
    });

    connect(ui->pb_lastCalTimePage, &QPushButton::clicked, this, [&](){
        if(ui->stackedWidget_3->currentIndex() == 1)
            ui->stackedWidget_3->setCurrentIndex(0);
    });

    connect(ui->pb_nextCalTimePage, &QPushButton::clicked, this, [&](){
        if(ui->stackedWidget_3->currentIndex() == 0)
            ui->stackedWidget_3->setCurrentIndex(1);
    });
}

void AutomaticMode::addSpeedClicked()
{
    emit addSpeedValue();
    ui->speed_flexibleLabel->setText(pendantInterfaceData.speedMutableLabel);
}

void AutomaticMode::decSpeedClicked()
{
    emit decSpeedValue();
    ui->speed_flexibleLabel->setText(pendantInterfaceData.speedMutableLabel);
}

void AutomaticMode::updateMantuableData()
{
    setSpeedValue(ui->speed_flexibleLabel);
}

void AutomaticMode::pushReturnMenu()
{
    emit returnMenu();
}

AutomaticMode::~AutomaticMode()
{
    delete ui;
}
